package math2;

import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.solvers.BracketingNthOrderBrentSolver;
import org.apache.commons.math3.analysis.solvers.BrentSolver;
import org.apache.commons.math3.analysis.solvers.UnivariateSolver;
import org.junit.Test;

import fem2.Debugger;

public class UnivariateSolverTest {

	@Test
	public void test1() {
		UnivariateFunction function = new UnivariateFunction() {

			@Override
			public double value(double x) {
				return Math.cos(x) / x - 2;
			}
		};

		final double relativeAccuracy = 1.0e-12;
		final double absoluteAccuracy = 1.0e-8;
		final int maxOrder = 5;
		UnivariateSolver solver = new BracketingNthOrderBrentSolver(relativeAccuracy,
				absoluteAccuracy, maxOrder);
		double x = solver.solve(50, function, 0, 1);

		Debugger.watch("x = ", x);

	}

	@Test
	public void test2() {
		UnivariateFunction function = new UnivariateFunction() {

			@Override
			public double value(double x) {
				return Math.cos(x) / x - 2;
			}
		};

		final double relativeAccuracy = 1.0e-12;
		final double absoluteAccuracy = 1.0e-8;
		UnivariateSolver solver = new BrentSolver(relativeAccuracy, absoluteAccuracy);
		double x = solver.solve(50, function, 0, 1);

		Debugger.watch("x = ", x);

	}
}
